package com.guigu.community.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.community.pojo.Procure;
import com.guigu.community.service.ProcureService;
import com.guigu.community.vo.ProcureVo;
import com.guigu.community.vo.PurchaseRequestVo2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/procure")
@RestController
@CrossOrigin
public class ProcureController {

    @Autowired
    private ProcureService procureService;

    @PostMapping("/submitPurchaseRequest")
    @ResponseBody
        public Map<String, Object> submitPurchaseRequest(@RequestBody PurchaseRequestVo2 purchaseRequestVo2) {
        Map<String, Object> result = new HashMap<>();
        boolean success = procureService.submitPurchaseRequest(purchaseRequestVo2);
        Procure procure = procureService.getProcureByRequest(purchaseRequestVo2);
        result.put("success", success);
        if (procure != null) {
            result.put("id", procure.getId());
        } else {
            // 处理获取采购单失败的情况，例如返回错误信息
            result.put("id", -1);
        }
        return result;
    }

    @PostMapping("/submitGoodsList")
    @ResponseBody
    public Map<String, Object> submitGoodsList(@RequestParam("parentId") Integer parentId, @RequestBody List<ProcureVo> procureVoList) {
        Map<String, Object> result = new HashMap<>();
        boolean success = procureService.submitGoodsList(parentId, procureVoList);
        result.put("success", success);
        return result;
    }

    @PostMapping("/selectAll/{currentPage}/{pageSize}")
    @ResponseBody
    public PageInfo<Procure> selectAll(@PathVariable Integer currentPage,@PathVariable Integer pageSize,@RequestBody ProcureVo procureVo) {
        PageInfo<Procure> pageInfo = procureService.getProcureList(currentPage, pageSize, procureVo);
        return pageInfo;
    }


    @PostMapping("/approve/{id}")
    public ResponseEntity<String> approveProcure(@PathVariable("id") Integer id) {
        boolean success = procureService.approveProcure(id);
        if (success) {
            return ResponseEntity.ok("审核通过");
        } else {
            return ResponseEntity.status(500).body("审核通过失败");
        }
    }

    @PostMapping("/reject/{id}")
    public ResponseEntity<String> rejectProcure(@PathVariable("id") Integer id, @RequestBody Map<String, String> request) {
        String reason = request.get("reason");
        boolean success = procureService.rejectProcure(id, reason);
        if (success) {
            return ResponseEntity.ok("审核拒绝成功");
        } else {
            return ResponseEntity.status(500).body("审核拒绝失败");
        }
    }

    @PostMapping("/selectAll2/{current}/{size}")
    @ResponseBody
    public PageInfo<Procure> selectAll2(@PathVariable Integer current,@PathVariable Integer size,@RequestBody ProcureVo procureVo) {
        PageInfo<Procure> pageInfo = procureService.getProcureList2(current, size, procureVo);
        return pageInfo;
    }
}